package com.topera.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.topera.eduservice.client.VodClient;
import com.topera.eduservice.entity.EduVideo;
import com.topera.eduservice.mapper.EduVideoMapper;
import com.topera.eduservice.service.EduVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author puzhenjiang
 * @since 2020-05-18
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Autowired
    private VodClient vodClient;

    @Override
    public void removeVideoByCourseId(String id) {

        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",id);
        //wrapper.select("video_source_id"); 查询指定列
        //根据课程id 获取视频id集合
        List<EduVideo> videoList =  baseMapper.selectList(wrapper);
        List<String> videoIdList = videoList.stream().filter((y)-> !StringUtils.isEmpty(y.getVideoSourceId())).map((x)->x.getVideoSourceId()).collect(Collectors.toList());

        //调用vodClient中的方法执行删除多个视频的方法
        if(videoIdList.size()>0){
            vodClient.deleteBatch(videoIdList);
        }

        baseMapper.delete(wrapper);
    }
}
